package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.modules.doctor.entity.DoctorConfig;
import org.jeecg.common.modules.doctor.entity.DoctorWxUserinfoRelation;
import org.jeecg.common.modules.doctor.mapper.DoctorWxUserinfoRelationMapper;
import org.jeecg.common.modules.doctor.service.IDoctorConfigService;
import org.jeecg.common.modules.doctor.service.IDoctorWxUserinfoRelationService;
import org.jeecg.common.modules.im.ImService;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: doctor_account_flow
 * @Author: jeecg-boot
 * @Date:   2020-08-22
 * @Version: V1.0
 */
@Service
public class IDoctorWxUserinfoRelationServiceImpl extends ServiceImpl<DoctorWxUserinfoRelationMapper, DoctorWxUserinfoRelation> implements IDoctorWxUserinfoRelationService {

    @Resource
    private IDoctorConfigService doctorConfigService;
    @Resource
    private IWxUserinfoService wxUserinfoService;
    @Resource
    private ImService imService;

    @Override
    public List<DoctorWxUserinfoRelation> getLimitInfo(Integer start, Integer end){
        LambdaQueryWrapper<DoctorWxUserinfoRelation> query = Wrappers.lambdaQuery();
        query.isNull(DoctorWxUserinfoRelation::getImGroupId);
        query.between(DoctorWxUserinfoRelation::getId,start,end);
        return list(query);
    }
    @Override
    public DoctorWxUserinfoRelation initRelation(String doctorId, String openid,String groupId) {
        DoctorConfig doctorConfig = doctorConfigService.getByDoctorId(doctorId,false);
        DoctorWxUserinfoRelation relation = new DoctorWxUserinfoRelation();
        relation.setDoctorId(doctorId);
        relation.setOpenid(openid);
        relation.setCreatedAt(new Date());
        relation.setTextNumber(Objects.isNull(doctorConfig.getFreeMessageCount()) ? 0 : doctorConfig.getFreeMessageCount());
        relation.setVideoNumber(0);
        relation.setImGroupId(groupId);
        save(relation);
        return relation;
    }

    @Override
    public DoctorWxUserinfoRelation getByDoctorAndOpenid(String doctorId, String openid) {
        LambdaQueryWrapper<DoctorWxUserinfoRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorWxUserinfoRelation::getDoctorId,doctorId);
        query.eq(DoctorWxUserinfoRelation::getOpenid,openid);
        return getOne(query);
    }

    @Override
    public DoctorWxUserinfoRelation getByGroupId(String groupId) {
        LambdaQueryWrapper<DoctorWxUserinfoRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorWxUserinfoRelation::getImGroupId,groupId);
        return getOne(query);
    }


    @Override
    public void updateTextNumber(String id,int count) {
        baseMapper.updateTextNumber(id,count);
    }

    @Override
    public List<DoctorWxUserinfoRelation> followDoctor(String smOpenid){
        //检测当前用户是否有openid
        WxUserinfo wxUserinfo = wxUserinfoService.getBySmOpenid(smOpenid);
        LambdaQueryWrapper<DoctorWxUserinfoRelation> query = Wrappers.lambdaQuery();
        if(wxUserinfo.getOpenid() != null){
            query.eq(DoctorWxUserinfoRelation::getOpenid,wxUserinfo.getOpenid());
        }else{
            query.eq(DoctorWxUserinfoRelation::getSmOpenid,smOpenid);
        }

        return list(query);
    }

    @Override
    public Integer getConsultNum(String doctorId){
        LambdaQueryWrapper<DoctorWxUserinfoRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorWxUserinfoRelation::getDoctorId,doctorId);
        query.isNotNull(DoctorWxUserinfoRelation::getImGroupId);
        return count(query);
    }
}
